package com.ruoyi.crop.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.crop.api.domain.OutBound;
import com.ruoyi.crop.api.domain.SrcLogistics;
import com.ruoyi.crop.service.LogisticsService;
import com.ruoyi.crop.service.OutBoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : Guosir
 * @date : 2024-07-29 15:35
 * @description : 物流列表
 **/
@RestController
@RequestMapping("wuliu")
public class LogisticsController extends BaseController {

    @Autowired
    private LogisticsService logisticsService;

    @Autowired
    private OutBoundService outBoundService;

    /**
     * 查询物流记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SrcLogistics srcLogistics)
    {
        startPage();
        List<SrcLogistics> list = logisticsService.listAll(srcLogistics);
        return getDataTable(list);
    }



    /**
     * 获取物流记录详细信息
     */
    @GetMapping(value = "/{logisticsId}")
    public AjaxResult getInfo(@PathVariable("logisticsId") Long logisticsId)
    {
        return success(logisticsService.getById(logisticsId));
    }

    /**
     * 新增物流记录
     */
    @Log(title = "物流记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcLogistics srcLogistics)
    {
        return toAjax(logisticsService.save(srcLogistics));
    }

    /**
     * 修改物流记录
     */
    @Log(title = "物流记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcLogistics srcLogistics)
    {
        return toAjax(logisticsService.updateById(srcLogistics));
    }

    /**
     * 删除物流记录
     */
    @Log(title = "物流记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logisticsIds}")
    public AjaxResult remove(@PathVariable Long[] logisticsIds)
    {
        return toAjax(logisticsService.removeBatchByIds(Arrays.asList(logisticsIds)));
    }


    /**
     * 入库
     */


    @Transactional
    @PostMapping("rk")
    public AjaxResult rk(SrcLogistics srcLogistics){
        if(srcLogistics.getStatus()!=2){
            return error("物流正在运输中,无法入库");
        }
        OutBound outBound = new OutBound();
        outBound.setBarCode(srcLogistics.getBarCode());
        outBound.setProductName(srcLogistics.getGoodsType());
        outBound.setMaterialLotId(srcLogistics.getMaterialLotId());
        outBound.setOutboundParty(srcLogistics.getOutboundParty());
        outBound.setNumber(srcLogistics.getGoodsNum());
        outBound.setBeginTime(new Date());
        boolean save = outBoundService.save(outBound);
        if(save){
            logisticsService.deleteById(srcLogistics.getLogisticsId());
        }else{
            throw new RuntimeException("入库失败");
        }
        return success();
    }

}
